package flc.ast.activity;

import androidx.annotation.NonNull;
import androidx.appcompat.app.AppCompatActivity;
import androidx.recyclerview.widget.LinearLayoutManager;

import android.app.Dialog;
import android.graphics.Bitmap;
import android.graphics.Color;
import android.os.Bundle;
import android.os.Handler;
import android.util.DisplayMetrics;
import android.view.Gravity;
import android.view.LayoutInflater;
import android.view.View;
import android.view.Window;
import android.view.WindowManager;
import android.widget.ImageView;
import android.widget.SeekBar;
import android.widget.TextView;

import com.blankj.utilcode.util.ImageUtils;
import com.blankj.utilcode.util.ToastUtils;
import com.chad.library.adapter.base.BaseQuickAdapter;
import com.vilyever.drawingview.brush.drawing.PenBrush;

import java.util.ArrayList;
import java.util.List;

import flc.ast.BaseAc;
import flc.ast.HomeActivity;
import flc.ast.R;
import flc.ast.adapter.ColorAdapter;
import flc.ast.bean.MyColorBean;
import flc.ast.databinding.ActivityDrawBinding;
import flc.ast.utils.Content;
import io.reactivex.rxjava3.core.ObservableEmitter;
import stark.common.basic.event.EventStatProxy;
import stark.common.basic.utils.FileUtil;
import stark.common.basic.utils.RxUtil;

public class DrawActivity extends BaseAc<ActivityDrawBinding> {

    public static int imgBg;
    private PenBrush penBrush;
    private int paintWidth = 15;//画笔宽度
    private int eraserWidth = 15;//橡皮擦宽度
    private ColorAdapter colorAdapter;
    private int oldColorPos = 0;
    private Dialog myDeleteDialog,myTipDialog;


    @Override
    protected int onCreate() {
        return R.layout.activity_draw;
    }

    @Override
    public void onBackPressed() {
       myTipDialog.show();
    }

    @Override
    protected void initView() {
        EventStatProxy.getInstance().statEvent1(this, mDataBinding.event1Container);
        mDataBinding.ivDrawBack.setOnClickListener(new View.OnClickListener() {
            @Override
            public void onClick(View view) {
                onBackPressed();
            }
        });
        mDataBinding.ivDrawSave.setOnClickListener(this);
        mDataBinding.ivDrawBrush.setOnClickListener(this);
        mDataBinding.ivDrawColor.setOnClickListener(this);
        mDataBinding.ivDrawEraser.setOnClickListener(this);
        mDataBinding.ivDrawClear.setOnClickListener(this);
        mDataBinding.tvDrawClearBoard.setOnClickListener(this);
        mDataBinding.rvDrawColorList.setLayoutManager(new LinearLayoutManager(mContext, LinearLayoutManager.HORIZONTAL, false));
        colorAdapter = new ColorAdapter();
        mDataBinding.rvDrawColorList.setAdapter(colorAdapter);
        colorAdapter.setOnItemClickListener(this);
        //清空弹框
        deleteDialog();
        tipDialog();
    }

    @Override
    protected void onClickCallback(View v) {
        switch (v.getId()){
            case R.id.ivDrawSave://保存
                saveImg();
                break;
            case R.id.ivDrawBrush://画笔
                clearView();
                mDataBinding.ivDrawBrush.setImageResource(R.drawable.huabi1);
                mDataBinding.llDrawBrush.setVisibility(View.VISIBLE);
                break;
            case R.id.ivDrawColor://颜色
                clearView();
                mDataBinding.ivDrawColor.setImageResource(R.drawable.yanse1);
                mDataBinding.rvDrawColorList.setVisibility(View.VISIBLE);
                break;
            case R.id.ivDrawEraser://橡皮擦
                clearView();
                mDataBinding.ivDrawEraser.setImageResource(R.drawable.xiangpi1);
                mDataBinding.llDrawEraser.setVisibility(View.VISIBLE);
                penBrush.setIsEraser(true);
                penBrush.setSize(eraserWidth);
                break;
            case R.id.ivDrawClear://清空
                clearView();
                mDataBinding.ivDrawClear.setImageResource(R.drawable.qingkong1);
                mDataBinding.tvDrawClearBoard.setVisibility(View.VISIBLE);
                break;
            case R.id.tvDrawClearBoard://清空画板
                myDeleteDialog.show();
                break;
            //清空弹框
            case R.id.ivDialogDeleteCancel://取消
                myDeleteDialog.dismiss();
                break;
            case R.id.ivDialogDeleteRight://确定
                myDeleteDialog.dismiss();
                mDataBinding.myDrawingView.clear();
                break;
            //提示弹框
            case R.id.ivDialogTipCancel://取消
                myTipDialog.dismiss();
                break;
            case R.id.ivDialogTipRight://确定
                myTipDialog.dismiss();
                finish();
                break;
        }
    }

    @Override
    protected void onItemClickCallback(@NonNull BaseQuickAdapter<?, ?> adapter, @NonNull View view, int position) {
        colorAdapter.getItem(oldColorPos).setSelect(false);
        oldColorPos = position;
        colorAdapter.getItem(position).setSelect(true);
        colorAdapter.notifyDataSetChanged();
        penBrush.setColor(Color.parseColor(colorAdapter.getItem(position).getColor()));//默认画笔颜色
    }

    @Override
    protected void initData() {
        mDataBinding.ivDrawImgBg.setImageResource(imgBg);
        initPen();
        List<MyColorBean> listColor = new ArrayList<>();
        listColor.add(new MyColorBean("#333333", true));
        listColor.add(new MyColorBean("#D8D8D8", false));
        listColor.add(new MyColorBean("#D0B5A4", false));
        listColor.add(new MyColorBean("#F5C6BF", false));
        listColor.add(new MyColorBean("#FA8EA5", false));
        listColor.add(new MyColorBean("#B17DD2", false));
        listColor.add(new MyColorBean("#A5201B", false));
        listColor.add(new MyColorBean("#5FBC8F", false));
        listColor.add(new MyColorBean("#3F68BC", false));
        listColor.add(new MyColorBean("#9F45F1", false));
        listColor.add(new MyColorBean("#E85C90", false));
        listColor.add(new MyColorBean("#C0C0C0", false));
        colorAdapter.setList(listColor);
    }

    //初始化画笔
    private void initPen() {
        penBrush = PenBrush.defaultBrush();
        penBrush.setColor(Color.parseColor("#333333"));//默认画笔颜色
        penBrush.setSize(15);//默认画笔大小
        mDataBinding.myDrawingView.setBrush(penBrush);
        //设置画笔大小
        mDataBinding.seekbarBrush.setMax(50);
        mDataBinding.seekbarBrush.setProgress(5);
        mDataBinding.seekbarBrush.setOnSeekBarChangeListener(new SeekBar.OnSeekBarChangeListener() {
            @Override
            public void onProgressChanged(SeekBar seekBar, int i, boolean b) {
                //画笔擦大小
                paintWidth = 5 + i * 2;
                penBrush.setSize(paintWidth);
            }

            @Override
            public void onStartTrackingTouch(SeekBar seekBar) {

            }

            @Override
            public void onStopTrackingTouch(SeekBar seekBar) {

            }
        });
        //设置橡皮擦大小
        mDataBinding.seekbarEraser.setMax(50);
        mDataBinding.seekbarEraser.setProgress(5);
        mDataBinding.seekbarEraser.setOnSeekBarChangeListener(new SeekBar.OnSeekBarChangeListener() {
            @Override
            public void onProgressChanged(SeekBar seekBar, int i, boolean b) {
                //画笔擦大小
                eraserWidth = 5 + i * 2;
                penBrush.setSize(eraserWidth);
            }

            @Override
            public void onStartTrackingTouch(SeekBar seekBar) {

            }

            @Override
            public void onStopTrackingTouch(SeekBar seekBar) {

            }
        });
    }

    //保存图片
    private void saveImg() {
        showDialog(getString(R.string.save_ing));
        new Handler().postDelayed(new Runnable() {
            @Override
            public void run() {
                RxUtil.create(new RxUtil.Callback<String>() {

                    @Override
                    public void doBackground(ObservableEmitter<String> emitter) {
                        Bitmap bitmap = ImageUtils.view2Bitmap(mDataBinding.rlDrawSave);
                        //保存到相册
                        ImageUtils.save2Album(bitmap, Bitmap.CompressFormat.PNG);
                        //保存到我的作品
                        String filePath = FileUtil.generateFilePath(Content.myDraw, ".png");
                        ImageUtils.save(bitmap, filePath, Bitmap.CompressFormat.PNG);
                        emitter.onNext("");
                    }

                    @Override
                    public void accept(String path) {
                        dismissDialog();
                        ToastUtils.showShort(R.string.save_suc);
                        startActivity(HomeActivity.class);
                    }
                });
            }
        }, 1000);
    }

    private void clearView(){
        penBrush.setIsEraser(false);
        penBrush.setSize(paintWidth);
        mDataBinding.ivDrawBrush.setImageResource(R.drawable.huabi2);
        mDataBinding.ivDrawColor.setImageResource(R.drawable.yanse2);
        mDataBinding.ivDrawEraser.setImageResource(R.drawable.xiangpi2);
        mDataBinding.ivDrawClear.setImageResource(R.drawable.qingkong2);
        mDataBinding.llDrawBrush.setVisibility(View.GONE);
        mDataBinding.rvDrawColorList.setVisibility(View.GONE);
        mDataBinding.llDrawEraser.setVisibility(View.GONE);
        mDataBinding.tvDrawClearBoard.setVisibility(View.GONE);
    }

    //清空弹框
    private void deleteDialog() {
        myDeleteDialog = new Dialog(mContext, R.style.DialogStyle);
        View inflate = LayoutInflater.from(mContext).inflate(R.layout.dialog_delete, null);
        myDeleteDialog.setContentView(inflate);
        myDeleteDialog.setCancelable(true);
        Window dialogWindow = myDeleteDialog.getWindow();
        //设置Dialog从窗体底部弹出
        dialogWindow.setGravity(Gravity.BOTTOM);
        DisplayMetrics dm = getResources().getDisplayMetrics();
        //获得窗体的属性
        WindowManager.LayoutParams lp = dialogWindow.getAttributes();
        lp.width = (int) (dm.widthPixels * 0.9);
        // 将属性设置给窗体
        dialogWindow.setAttributes(lp);
        ImageView cancel = inflate.findViewById(R.id.ivDialogDeleteCancel);
        ImageView right = inflate.findViewById(R.id.ivDialogDeleteRight);
        TextView text = inflate.findViewById(R.id.tvDialogDeleteText);
        cancel.setOnClickListener(this);
        right.setOnClickListener(this);
    }

    //提示弹框
    private void tipDialog() {
        myTipDialog = new Dialog(mContext, R.style.DialogStyle);
        View inflate = LayoutInflater.from(mContext).inflate(R.layout.dialog_tip, null);
        myTipDialog.setContentView(inflate);
        myTipDialog.setCancelable(true);
        Window dialogWindow = myTipDialog.getWindow();
        //设置Dialog从窗体底部弹出
        dialogWindow.setGravity(Gravity.BOTTOM);
        DisplayMetrics dm = getResources().getDisplayMetrics();
        //获得窗体的属性
        WindowManager.LayoutParams lp = dialogWindow.getAttributes();
        lp.width = (int) (dm.widthPixels * 0.9);
        // 将属性设置给窗体
        dialogWindow.setAttributes(lp);
        ImageView cancel = inflate.findViewById(R.id.ivDialogTipCancel);
        ImageView right = inflate.findViewById(R.id.ivDialogTipRight);
        cancel.setOnClickListener(this);
        right.setOnClickListener(this);
    }

}